package com.cimu.quartz.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.cimu.quartz.entity.ScheduleJob;
import com.cimu.quartz.enums.ScheduleStatusEnum;
import com.cimu.quartz.mapper.ScheduleJobMapper;
import com.cimu.quartz.service.ScheduleJobService;
import com.cimu.quartz.utils.ScheduleUtils;
import org.apache.commons.lang.StringUtils;
import org.quartz.CronTrigger;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ScheduleJobServiceImpl extends ServiceImpl<ScheduleJobMapper, ScheduleJob> implements ScheduleJobService {
	@Autowired
	private Scheduler scheduler;

	/**
	 * 项目启动时，初始化定时器
	 */
	@PostConstruct
	public void init(){
		List<ScheduleJob> scheduleJobList = this.selectList(null);
		for(ScheduleJob scheduleJob : scheduleJobList){
			CronTrigger cronTrigger = ScheduleUtils.getCronTrigger(scheduler, scheduleJob.getJobId());
			//如果不存在，则创建
			if(cronTrigger == null) {
				ScheduleUtils.createScheduleJob(scheduler, scheduleJob);
			}else {
				ScheduleUtils.updateScheduleJob(scheduler, scheduleJob);
			}
		}
	}

	@Override
	public Page<ScheduleJob> queryPage(Page<ScheduleJob> page,
									   ScheduleJob scheduleJobLog) {
		EntityWrapper<ScheduleJob> ew = new EntityWrapper<ScheduleJob>();
		ew.like(StringUtils.isNotBlank(scheduleJobLog.getBeanName()),"bean_name",scheduleJobLog.getBeanName());
		ew.orderBy("job_id",false);
		return this.selectPage(page,ew);
	}


	@Override
	@Transactional(rollbackFor = Exception.class)
	public void save(ScheduleJob scheduleJob) {
		scheduleJob.setCreateTime(new Date());
		scheduleJob.setStatus(ScheduleStatusEnum.NORMAL.getValue());
		this.insert(scheduleJob);

		ScheduleUtils.createScheduleJob(scheduler, scheduleJob);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void update(ScheduleJob scheduleJob) {
		if(null == selectById(scheduleJob.getJobId())){
			throw new RuntimeException("数据不存在");
		}
		ScheduleUtils.updateScheduleJob(scheduler, scheduleJob);

		this.updateById(scheduleJob);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteBatch(List<Long> jobIds) {
		for(Long jobId : jobIds){
			ScheduleUtils.deleteScheduleJob(scheduler, jobId);
		}
		//删除数据
		this.deleteBatchIds(jobIds);
	}

	@Override
	public int updateBatch(List<Long> jobIds, String status){
		Map<String, Object> map = new HashMap<>();
		map.put("list", jobIds);
		map.put("status", status);
		return baseMapper.updateBatch(map);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void run(List<Long> jobIds) {
		for(Long jobId : jobIds){
			ScheduleUtils.run(scheduler, this.selectById(jobId));
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void pause(List<Long> jobIds) {
		for(Long jobId : jobIds){
			ScheduleUtils.pauseJob(scheduler, jobId);
		}

		updateBatch(jobIds, ScheduleStatusEnum.PAUSE.getValue());
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void resume(List<Long> jobIds) {
		for(Long jobId : jobIds){
			ScheduleUtils.resumeJob(scheduler, jobId);
		}

		updateBatch(jobIds, ScheduleStatusEnum.NORMAL.getValue());
	}

}
